﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SchoolLibrary.Desktop.Guide
{
    public abstract partial class GuideForm<T> : Form
    {
        private const int INDENT = 20;
        private const int SMALL_INDENT = 10;

        private DataTable table;
        private T selectedObject;
        private ViewModeEnum viewMode;
        private List<T> data;

        public GuideForm(ViewModeEnum mode)
        {
            this.viewMode = mode;
            InitializeComponent();
            initTable();
            this.addButton.Visible = mode == ViewModeEnum.EDIT;
            this.editButton.Visible = mode == ViewModeEnum.EDIT;
            this.deleteButton.Visible = mode == ViewModeEnum.EDIT;
        }

        public GuideForm() : this(ViewModeEnum.EDIT) { }
        

        public void initData()
        {
            table.Rows.Clear();

            data = findAll();

            foreach (T value in data)
            {
                object[] row = createRow(value);
                DataRow newRow = table.NewRow();
                for (int i = 0; i < row.Length; i++)
                {
                    newRow[i] = row[i];
                }
                table.Rows.Add(newRow);
            }
        }

        private void initTable() 
        {
            table = new DataTable();

            string[] columnNames = getColumnNames();

            foreach (string columnName in columnNames)
            {
                table.Columns.Add(columnName);
            }

            dataGridView.DataSource = table;


            foreach (DataGridViewColumn column in dataGridView.Columns)
            {
                column.SortMode = DataGridViewColumnSortMode.NotSortable;
            }

            dataGridView.Columns[0].Visible = false;
            dataGridView.Width = getPreferedTableWidth() + INDENT;
            this.Width = dataGridView.Width + SMALL_INDENT;
        }

        private void dataGridView_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (viewMode == ViewModeEnum.SELECT && dataGridView.SelectedRows.Count == 1)
            {
                string id = (string)dataGridView.SelectedRows[0].Cells[0].Value;
                selectedObject = findById(Int64.Parse(id));
                this.Close();
            }
        }

        public T SelectedObject
        {
            get { return selectedObject; }
        }

        private int getPreferedTableWidth()
        {
            int width = 0;
            foreach (DataGridViewColumn column in dataGridView.Columns)
            {
                if (column.Visible)
                {
                    width += column.Width;
                }                
            }
            return width;
        }

        private int getPreferedTableHight()
        {
            int hight = 0;
            foreach (DataGridViewRow row in dataGridView.Rows)
            {
                if (row.Visible)
                {
                    hight += row.Height;
                }
            }
            return hight + dataGridView.ColumnHeadersHeight;
        }

        private void GuideForm_Shown(object sender, EventArgs e)
        {
            dataGridView.Columns[0].Visible = false;
        }

        protected abstract string[] getColumnNames();

        protected abstract List<T> findAll();

        protected abstract T findById(long id);

        protected abstract object[] createRow(T value);

        protected abstract void addButton_Click(object sender, EventArgs e);

        protected abstract void editButton_Click(object sender, EventArgs e);

        protected abstract void deleteButton_Click(object sender, EventArgs e);

        protected long getSelectedRowId()
        {
            long id = -1;
            if (this.dataGridView.SelectedRows.Count == 1)
            {
                id = Int64.Parse(this.dataGridView.SelectedRows[0].Cells[0].Value.ToString());
            }
            return id;
        }

    }

}
